package com.xingxue.module.user.controller;

import com.xingxue.module.user.entity.*;
import com.xingxue.module.user.repository.QuestionDetailRepository;
import com.xingxue.module.user.repository.UserRepository;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 用户api
 *
 * @author yihang
 */
@RestController
public class UserController {

    @Autowired
    private UserRepository userRepository;

    @Autowired
    private QuestionDetailRepository questionDetailRepository;


    @ApiOperation(value = "根据用户id查询用户信息", httpMethod = "GET", produces = "application/json")
    @ApiResponses(value = {@ApiResponse(code = 200, message = "OK")})
    @GetMapping("/api/{userId}/question/{correct}")
    public QuestionResult findCorrectQuestion(
            @PathVariable @ApiParam(value = "用户id", required = true) int userId,
            @PathVariable @ApiParam(value = "是否正确", required = true) boolean correct) {
        if(correct) {
            List<Integer> questionIds = questionDetailRepository.findCorrectQuestion(userId);
            return new QuestionResult(true, "查询成功", questionIds);
        } else {
            List<Integer> questionIds = questionDetailRepository.findWrongQuestion(userId);
            return new QuestionResult(true, "查询成功", questionIds);
        }
    }

    /**
     * 根据用户id查询用户
     *
     * @param id 用户id
     * @return 用户
     */
    @ApiOperation(value = "根据用户id查询用户信息", httpMethod = "GET", produces = "application/json")
    @ApiResponses(value = {@ApiResponse(code = 200, message = "OK")})
    @GetMapping("/api/user/{id}")
    public UserResult findOne(@PathVariable @ApiParam(value = "用户id", required = true) int id) {
        User user = userRepository.findOne(id);
        return new UserResult(true, "查询用户成功", user);
    }

    /**
     * 根据微信openId查询用户id
     *
     * @param openId 微信openId
     * @return 用户id
     */
    @ApiOperation(value = "根据微信openId查询系统内的用户id", httpMethod = "GET", produces = "application/json")
    @ApiResponses(value = {@ApiResponse(code = 200, message = "OK", response=UserIdResult.class)})
    @GetMapping("/api/public/get_userid")
    public UserIdResult findOne(@RequestParam @ApiParam(name = "openId", value = "微信openId", required = true) String openId) {
        User user = userRepository.findByOpenId(openId);
        return new UserIdResult(true, "查询用户ID成功", user != null ? user.getId() : null);
    }

    /**
     * 添加或修改微信用户
     *
     * @param user 用户信息
     * @return
     */
    @ApiOperation(value = "添加或修改微信用户", httpMethod = "POST", consumes = "application/json", produces = "application/json")
    @ApiResponses(value = {@ApiResponse(code = 200, message = "OK", response = UserIdResult.class)})
    @PostMapping("/api/user")
    public UserIdResult save(@RequestBody @ApiParam(value = "用户信息", required = true) User user) {
        if (user.getOpenId() == null) {
            return new UserIdResult(false, "微信openId不能为空", -1);
        }
        // 查询数据库用户对象old
        User old = userRepository.findByOpenId(user.getOpenId());
        if (old != null) {
            // 如果old对象存在，将新的信息赋值给old对象，然后更新
            old.setAvatarUrl(user.getAvatarUrl());
            old.setCountry(user.getCountry());
            old.setProvince(user.getProvince());
            old.setCity(user.getCity());
            old.setLanguage(user.getLanguage());
            old.setGender(user.getGender());
            old.setNickName(user.getNickName());
            userRepository.save(old);
            return new UserIdResult(true, "用户修改成功", old.getId());
        } else {
            userRepository.save(user);
            return new UserIdResult(true, "用户添加成功", user.getId());
        }
    }
}
